package org.baicaixiaozhan.tenet.reflection.limiter;

import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.LastModified;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * DESC:
 *
 * @author : baicaixiaozhan
 * @since : 2021-05-22
 */
public class MyHandlerAdapter implements HandlerAdapter {
    @Override
    public boolean supports(Object handler) {
        return handler instanceof Controller;
    }

    @Override
    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response,
                               Object handler) throws Exception {
        if (this.supports(handler)) {
            return ((Controller) handler).handleRequest(request, response);
        }
        return null;
    }

    /**
     * Same contract as for HttpServlet's {@code getLastModified} method.
     * Can simply return -1 if there's no support in the handler class.
     *
     * @param request current HTTP request
     * @param handler the handler to use
     * @return the lastModified value for the given handler
     * @see HttpServlet#getLastModified
     * @see LastModified#getLastModified
     */
    @Override
    public long getLastModified(HttpServletRequest request, Object handler) {
        return 0;
    }
}
